<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>临时车辆管理</el-breadcrumb-item>
      <el-breadcrumb-item>停车管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 添加区 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入车牌号码"
            v-model="carNum"
            class="input-with-select"
            clearable
            @clear="getCardList()"
          >
          </el-input>
        </el-col>

        <el-col :span="4">
          <el-button type="primary" @click="addTemp()">添加临时车辆</el-button>
        </el-col>
      </el-row>

      <!-- IC列表区域 -->
      <el-table :data="tempList" border stripe>
        <el-table-column prop="tempId" label="临时编号"> </el-table-column>
        <el-table-column prop="cardId" label="临时IC卡号"> </el-table-column>
        <el-table-column prop="carNum" label="车牌号码"> </el-table-column>
        <el-table-column
          prop="entryTime"
          :formatter="dateFormat1"
          label="入场日期"
        >
        </el-table-column>
        <el-table-column
          prop="outTime"
          :formatter="dateFormat2"
          label="出场日期"
        >
        </el-table-column>
        <el-table-column prop="tempMoney" label="停车费用"> </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <!-- 结算 -->
            <el-tooltip
              class="item"
              effect="dark"
              content="结算"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                circle
                @click="getMoney(scope.row.tempId)"
                :disabled="scope.row.outTime ? true : false"
              ></el-button>
            </el-tooltip>

            <!-- 删除 -->
            <el-tooltip
              class="item"
              effect="dark"
              content="删除"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                @click="removeTempById(scope.row.tempId)"
                :disabled="scope.row.tempMoney ? false : true"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  data() {
    return {
      carNum: '',
      tempList: [],
      total: null,
      //   当前的页数
      pageNum: 1,
      //   当前每页显示多少条数据
      pageSize: 10,
    }
  },
  mounted() {
    this.getTempList()
  },
  methods: {
    addTemp() {
      
      if(!this.carNum){
        this.$message.warning('请输入车牌号码')
        return;
      }

      this.$http.get('/temp/entry/' + this.carNum).then((result) => {
        this.getTempList()
        this.$message.success('添加成功')
        this.carNum = ''
      })
    },
    async getTempList() {
      const { data: res } = await this.$http.post('temp/list', {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      })
      this.total = res.total
      this.tempList = res.list
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize
      this.getTempList()
    },
    handleCurrentChange(newPageNum) {
      this.pageNum = newPageNum
      this.getTempList()
    },

    getMoney(tempId) {
      this.$http.post('/temp/out/' + tempId).then((result) => {
        this.getTempList()
        this.$message.success('结算成功')
      })
    },
    async removeTempById(tempId) {
      const confirmResult = await this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).catch((err) => err)
      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消删除，则返回值为字符串 cancel
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }

      const res = await this.$http.delete('temp/delete/' + tempId)
      if (res.code == 200) {
        this.$message.success('删除成功')
        this.getTempList()
      } else {
        this.$message.error('删除失败')
      }
    },
    dateFormat1(date) {
      return moment(date.entryTime).format('YYYY-MM-DD HH:mm:ss')
    },
    dateFormat2(date) {
      if (date.outTime) {
        return moment(date.outTime).format('YYYY-MM-DD HH:mm:ss')
      } else {
        return '未出场'
      }
    },
  },
}
</script>

<style lang="less" scoped></style>
